package com.origin.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.origin.file.constant.SysParamConstant;
import com.origin.file.service.IStorageService;
import com.origin.file.entity.domain.StorageBean;
import com.origin.file.entity.domain.SysParam;
import com.origin.file.mapper.StorageMapper;
import com.origin.file.mapper.SysParamMapper;
import com.origin.file.mapper.UserFileMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 马超
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class StorageService extends ServiceImpl<StorageMapper, StorageBean> implements IStorageService {
    @Resource
    StorageMapper storageMapper;
    @Resource
    SysParamMapper sysParamMapper;
    @Resource
    UserFileMapper userFileMapper;


    @Override
    public Long getTotalStorageSize(String userId) {
        LambdaQueryWrapper<StorageBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StorageBean::getUserId, userId);

        StorageBean storageBean = storageMapper.selectOne(lambdaQueryWrapper);
        Long totalStorageSize = null;
        if (storageBean == null || storageBean.getTotalStorageSize() == null) {
            LambdaQueryWrapper<SysParam> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(SysParam::getSysParamKey, "totalStorageSize");
            SysParam sysParam = sysParamMapper.selectOne(lambdaQueryWrapper1);
            totalStorageSize = Long.parseLong(sysParam.getSysParamValue());
            storageBean = new StorageBean();
            storageBean.setUserId(userId);
            storageBean.setTotalStorageSize(totalStorageSize);
            storageMapper.insert(storageBean);
        } else {
            totalStorageSize = storageBean.getTotalStorageSize();
        }

        if (totalStorageSize != null) {
            totalStorageSize = totalStorageSize * 1024 * 1024;
        }
        return totalStorageSize;
    }

    @Override
    public boolean checkStorage(String userId, Long fileSize) {
        StorageBean storageBean = storageMapper.selectOne(new LambdaQueryWrapper<StorageBean>().eq(StorageBean::getUserId, userId));
        if (storageBean == null) {
            SysParam sysParam = sysParamMapper
                    .selectOne(new LambdaQueryWrapper<SysParam>().eq(SysParam::getSysParamKey, SysParamConstant.TOTAL_STORAGE_SIZE));
            storageBean = new StorageBean();
            storageBean.setUserId(userId);
            storageBean.setTotalStorageSize(Long.parseLong(sysParam.getSysParamValue()));
            storageMapper.insert(storageBean);
        }

        long totalStorageSize = storageBean.getTotalStorageSize() * 1024 * 1024;

        Long storageSize = userFileMapper.selectStorageSizeByUserId(userId);
        if (storageSize == null) {
            storageSize = 0L;
        }
        return storageSize + fileSize <= totalStorageSize;
    }

}
